package com.schooldate.model.service.impl;

import com.schooldate.base.BaseSupportImpl;
import com.schooldate.model.entity.UserInfo;
import com.schooldate.model.service.UserInfoService;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by etund on 2015/3/13 0013.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class UserInfoServiceImpl  extends BaseSupportImpl<UserInfo> implements UserInfoService{

    @Override
    public List<UserInfo> getByCondition(UserInfo user) {
        String sql = "from UserInfo u where 1=1";
        if (user.getId()!=null)
            sql += " and u.id ="+user.getId();
        if (user.getNickName()!=null)
            sql += " and u.nickName = '"+user.getNickName()+"'";
        if(user.getEmail()!=null)
            sql += " and u.email = '"+ user.getEmail()+"'";
        if(user.getPassword()!=null)
            sql += " and u.password = '"+ user.getPassword()+"'";
        if(user.getGender()!=null)
            sql += " and u.gender = '"+ user.getGender()+"'";
        System.out.println(sql+"=========这是sql语句==========");
        return getSession().createQuery(sql).list();
    }

    @Override
    public UserInfo getByEmail(String email) {
        list =  this.getSession().createCriteria(UserInfo.class)
                                        .add(Restrictions.eq("email",email))
                                        .list();
       if (list.size()>0){
           return list.get(0);
       }
        return null;
    }

    @Override
    public UserInfo getByEmailAndPassword(String email, String password) {
        list =  this.getSession().createCriteria(UserInfo.class)
                .add(Restrictions.eq("email",email))
                .add(Restrictions.eq("password",password))
                .list();
        if (list.size()>0)
            return list.get(0);
        return null;
    }
//
    @Override
    public List<UserInfo> getByOrderFlowers() {
        return this.getSession().createCriteria(UserInfo.class)
                .addOrder(Order.desc("reciFlower"))
                .setMaxResults(20)
                .list();
    }

    @Override
    public List<UserInfo> getByLoginInfo() {
        return this.getSession().createCriteria(UserInfo.class)
                .add(Restrictions.eq("isLoginToday",true))
                .list();
    }

}
